package atm;

import java.util.Scanner;

/**
 * 多用户版本ATM
 */
public class ATM02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎进入XXX银行");
        String [][] users = {
                {"aaaaaa","111111","1000"},
                {"bbbbbb","222222","2000"},
                {"cccccc","333333","3000"},
                {"dddddd","444444","4000"},
                {"eeeeee","555555","5000"}
        };
        int index = 0;
        while(true){
            // 三次比较，如果连续失败则系统退出
            for(int i = 0;i < 3;i++){
                // 登录判断
                System.out.println("请输入账号：");
                String account = sc.next();
                System.out.println("请输入密码：");
                String pwd = sc.next();
                boolean isLogin = false;
                for(int j = 0;j < users.length;j++){
                    if(users[j][0].equals(account) && users[j][1].equals(pwd)){
                        System.out.println("登录成功");
                        isLogin = true;
                        index = j;
                        break;
                    }
                }
                if(isLogin){
                    break;
                }else{
                    System.out.println("登录失败，还剩"+(2 - i)+"次");
                    if(i == 2){
                        System.out.println("卡已被吞");
                        System.exit(0);
                    }
                }


            }


            while(true){
                // 如果登录成功，进入菜单页面
                System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-切换账号 6-退出");
                int input = sc.nextInt();

                if(input == 1){
                    System.out.println("请输入存的金额：");
                    double blance = sc.nextDouble();
                    users[index][2] = Double.parseDouble(users[index][2]) + blance + "";
                    System.out.println("存款成功");
                }else if(input == 2){
                    System.out.println("请输入取款的金额：");
                    double blance = sc.nextDouble();
                    if(blance > Double.parseDouble(users[index][2])){
                        System.out.println("余额不足");
                    }else{
                        users[index][2] = Double.parseDouble(users[index][2]) - blance + "";
                        System.out.println("取款成功");
                    }
                }else if(input == 3){
                    System.out.println("您的余额为："+users[index][2]);
                }else if(input == 4){
                    System.out.println("请输入对方账号：");
                    String otherAccount = sc.next();
                    // 不能转账给自己
                    if(otherAccount.equals(users[index][0])){
                        System.out.println("不能转给自己");
                        continue;
                    }
                    // 判断对方账号是否存在
                    boolean isExist = false;
                    int otherIndex = 0;
                    for(int i = 0;i < users.length;i++){
                        if(users[i][0].equals(otherAccount)){
                            isExist = true;
                            otherIndex = i;
                            break;
                        }
                    }
                    if(!isExist){
                        System.out.println("输入的对方账号错误");
                        continue;
                    }
                    System.out.println("请输入转账金额：");
                    double blance = sc.nextDouble();
                    // 判断余额是否足够
                    if(blance > Double.parseDouble(users[index][2])){
                        System.out.println("余额不足");
                        continue;
                    }
                    // 从当前账号上扣去转账的金额
                    users[index][2] = Double.parseDouble(users[index][2]) - blance + "";
                    // 将转账的金额加在对方账号上
                    users[otherIndex][2] = Double.parseDouble(users[otherIndex][2]) + blance + "";

                    System.out.println("转账成功");
                }else if(input == 5){
                    // 切换账号
                    break;
                }else if(input == 6){
                    // 系统结束
                    System.out.println("再见");
                    System.exit(0);
                }else{
                    System.out.println("输入有误");
                }
            }
        }



    }
}

